
use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1

append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"
replace treated=1 if d==2

append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_d_distrib_as_treated_20250319.dta"

gen NONEMP = 1 if ABS==1
	recode NONEMP (.=0) if ABS==0
	
set seed 12345

keep if d<=6
*keep if d>=3 & d<=6
*keep if NONEMP !=.

gen month_shock = t-12*int(t/12)
	recode month_shock (0=12)
gen month_return = month_shock+d

*keep if treated == 1 | control == 1

forval i=1/8 {
	local j=`i'+8
	rename mean_fq`i'_w w_q`j'
	replace mean_fq`i'_FEj_imp =. if flag_missingFEj_fq`i'==1
	rename mean_fq`i'_FEj_imp FE_q`j'


}
forval i=1/8 {
	local j=8-`i'
	rename mean_lq`i'_w w_q`j'
	replace mean_lq`i'_FEj_imp =. if flag_missingFEj_lq`i'==1
	rename mean_lq`i'_FEj_imp FE_q`j'
}


* reshape *
capture drop FEj_t4 FEj_t5 FEj_t6 FEj_t7 FEj_t8 FEj_t9 FEj_t10 FEj_t11 FEj_t12 
capture drop lnw_restr_t4 lnw_restr_t5 lnw_restr_t6 lnw_restr_t7 lnw_restr_t8 lnw_restr_t9 lnw_restr_t10 lnw_restr_t11 lnw_restr_t12 

reshape long w_q FE_q, i(anon) j(relative_time)


recode teaor1_h1 (-1=99) (.=100)

cap drop date
gen date=t+(relative_time-8)*3
replace date=t+d+(relative_time-9)*3 if relative_time>=8

rename kor kor_orig

rename ev ev_orig
rename t t_orig
rename date t

gen ev=floor((t-1)/12)+2003
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(vallazon1 wtip1 wh1 feor1_h2 kor)
replace wh1=. if wh1>40
rename vallazon1 vallazon
merge m:1 vallazon ev using "/homeKRTK/health_shocks/firmquality.dta", nogen keep(master match)

merge 1:1 anon t using "/homePCIESSD800/Admin3/Output_data/Alap/admin3_passziv_kieg_H1_v1.dta", nogen keepusing(passziv_ho)
replace passziv_ho=0 if passziv_ho==.

by anon, sort: gegen full_d=min(d)
drop if full_d==. //drop anons who are never in our baseline sample

rename t date
rename t_orig t

by anon, sort: gegen full_t=min(t)

gen sl_relevant1_=passziv_ho if date>=full_t & date<=full_t+1
by anon, sort: gegen sl_relevant1=total(sl_relevant1_)
drop sl_relevant1_

drop if d==. //back to baseline+falsification sample
drop if d>6

keep if sl_relevant1>0 | control==1 //sample restriction!!!


***Check original DID
global ct_ind 		i.kor i.occupation i.teaor1_h1 ferfi mean_lag_w	// individual-level control variables (non-health) //
global ct_health 	log_health_12mo A_12mo-V_12mo			// individual-level control variables (health) //
global ct_FEi		FEi						// raw AKM individual FE //
global ct_FEi_imp	FEi_imp flag_missingFEi				// imputed AKM individual FE and its missing flag //
global ct_firm 		size_pre_imp FO_pre_imp logmean_w_pre_imp flag_missingsize_pre flag_missingFO_pre flag_missinglogmean_w_pre 	// firm-level control variables, corresponding to firm at t //
global ct_FEj		FEj_l4 FEj_l5 FEj_l6 FEj_l7 FEj_l8 FEj_l9 FEj_l10 FEj_l11 FEj_l12 					// raw AKM firm FE //
global ct_FEj_imp	flag_missingFEj_l4-flag_missingFEj_l12 FEj_l4_imp - FEj_l12_imp 		// imputed AKM firm FE and its missing flag //
global FEt		i.t						// d and time FE //
replace teaor1_h1=. if teaor1_h1<0

cap drop ABS
gen ABS=0
replace ABS=1 if d>=2 & d~=. & control==0
cap drop ACC
gen ACC=0
replace ACC=1 if control==0

cap drop post
cap drop ACCpost
cap drop ABSpost
gen post=1 if (relative_time==12)
replace post=0 if relative_time==7
gen ACCpost=ACC*post
gen ABSpost=ABS*post

cap erase /homeKRTK/health_shocks/EventStudy/log_IV_Marc2025.log
log using /homeKRTK/health_shocks/EventStudy/log_IV_Marc2025.log, append

ivregress 2sls w_q  $FEt $ct_ind $ct_health $ct_FEi_imp $ct_firm $ct_FEj_imp post (ABS ABSpost=ACC ACCpost) if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., rob
ivregress 2sls FE_q  $FEt $ct_ind $ct_health $ct_FEi_imp $ct_firm $ct_FEj_imp post (ABS ABSpost=ACC ACCpost) if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., rob

log close

cap drop post
cap drop ACCpost
cap drop ABSpost
gen post=1 if (relative_time==16)
replace post=0 if relative_time==7
gen ACCpost=ACC*post
gen ABSpost=ABS*post

****Input for Table A2

log using /homeKRTK/health_shocks/EventStudy/log_IV_Marc2025.log, append

ivregress 2sls w_q  $FEt $ct_ind $ct_health $ct_FEi_imp $ct_firm $ct_FEj_imp post (ABS ABSpost=ACC ACCpost) if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., rob
ivregress 2sls FE_q  $FEt $ct_ind $ct_health $ct_FEi_imp $ct_firm $ct_FEj_imp post (ABS ABSpost=ACC ACCpost) if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., rob

log close
